//
//  AuctionInfoObj.m
//
//  Created by lizhenqiang  on 15/8/9
//  Copyright (c) 2015 __MyCompanyName__. All rights reserved.
//

#import "AuctionInfoObj.h"
#import "AuctionInfoVehiclePics.h"


NSString *const kAuctionInfoObjVehiclePics = @"vehiclePics";
NSString *const kAuctionInfoObjShelfCode = @"shelfCode";
NSString *const kAuctionInfoObjCarColor = @"carColor";
NSString *const kAuctionInfoObjId = @"id";
NSString *const kAuctionInfoObjStartDate = @"startDate";
NSString *const kAuctionInfoObjStartPrice = @"startPrice";
NSString *const kAuctionInfoObjFirstPic = @"firstPic";
NSString *const kAuctionInfoObjMileageCount = @"mileageCount";
NSString *const kAuctionInfoObjState = @"state";
NSString *const kAuctionInfoObjAuctionTitle = @"auctionTitle";


@interface AuctionInfoObj ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation AuctionInfoObj

@synthesize vehiclePics = _vehiclePics;
@synthesize shelfCode = _shelfCode;
@synthesize carColor = _carColor;
@synthesize internalBaseClassIdentifier = _internalBaseClassIdentifier;
@synthesize startDate = _startDate;
@synthesize startPrice = _startPrice;
@synthesize firstPic = _firstPic;
@synthesize mileageCount = _mileageCount;
@synthesize state = _state;
@synthesize auctionTitle = _auctionTitle;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
    NSObject *receivedAuctionInfoVehiclePics = [dict objectForKey:kAuctionInfoObjVehiclePics];
    NSMutableArray *parsedAuctionInfoVehiclePics = [NSMutableArray array];
    if ([receivedAuctionInfoVehiclePics isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedAuctionInfoVehiclePics) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedAuctionInfoVehiclePics addObject:[AuctionInfoVehiclePics modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedAuctionInfoVehiclePics isKindOfClass:[NSDictionary class]]) {
       [parsedAuctionInfoVehiclePics addObject:[AuctionInfoVehiclePics modelObjectWithDictionary:(NSDictionary *)receivedAuctionInfoVehiclePics]];
    }

    self.vehiclePics = [NSArray arrayWithArray:parsedAuctionInfoVehiclePics];
            self.shelfCode =[NSString stringWithFormat:@"%@",[self objectOrNilForKey:kAuctionInfoObjShelfCode fromDictionary:dict]];
            self.carColor = [NSString stringWithFormat:@"%@",[self objectOrNilForKey:kAuctionInfoObjCarColor fromDictionary:dict]];
            self.internalBaseClassIdentifier = [[self objectOrNilForKey:kAuctionInfoObjId fromDictionary:dict] doubleValue];
            self.startDate = [NSString stringWithFormat:@"%@",[self objectOrNilForKey:kAuctionInfoObjStartDate fromDictionary:dict]];
            self.startPrice = [NSString stringWithFormat:@"%@",[self objectOrNilForKey:kAuctionInfoObjStartPrice fromDictionary:dict]];
            self.firstPic = [NSString stringWithFormat:@"%@",[self objectOrNilForKey:kAuctionInfoObjFirstPic fromDictionary:dict]];
            self.mileageCount = [NSString stringWithFormat:@"%@",[self objectOrNilForKey:kAuctionInfoObjMileageCount fromDictionary:dict]];
            self.state = [NSString stringWithFormat:@"%@",[self objectOrNilForKey:kAuctionInfoObjState fromDictionary:dict]];
            self.auctionTitle = [NSString stringWithFormat:@"%@",[self objectOrNilForKey:kAuctionInfoObjAuctionTitle fromDictionary:dict]];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    NSMutableArray *tempArrayForVehiclePics = [NSMutableArray array];
    for (NSObject *subArrayObject in self.vehiclePics) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForVehiclePics addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForVehiclePics addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForVehiclePics] forKey:kAuctionInfoObjVehiclePics];
    [mutableDict setValue:self.shelfCode forKey:kAuctionInfoObjShelfCode];
    [mutableDict setValue:self.carColor forKey:kAuctionInfoObjCarColor];
    [mutableDict setValue:[NSNumber numberWithDouble:self.internalBaseClassIdentifier] forKey:kAuctionInfoObjId];
    [mutableDict setValue:self.startDate forKey:kAuctionInfoObjStartDate];
    [mutableDict setValue:self.startPrice forKey:kAuctionInfoObjStartPrice];
    [mutableDict setValue:self.firstPic forKey:kAuctionInfoObjFirstPic];
    [mutableDict setValue:self.mileageCount forKey:kAuctionInfoObjMileageCount];
    [mutableDict setValue:self.state forKey:kAuctionInfoObjState];
    [mutableDict setValue:self.auctionTitle forKey:kAuctionInfoObjAuctionTitle];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.vehiclePics = [aDecoder decodeObjectForKey:kAuctionInfoObjVehiclePics];
    self.shelfCode = [aDecoder decodeObjectForKey:kAuctionInfoObjShelfCode];
    self.carColor = [aDecoder decodeObjectForKey:kAuctionInfoObjCarColor];
    self.internalBaseClassIdentifier = [aDecoder decodeDoubleForKey:kAuctionInfoObjId];
    self.startDate = [aDecoder decodeObjectForKey:kAuctionInfoObjStartDate];
    self.startPrice = [aDecoder decodeObjectForKey:kAuctionInfoObjStartPrice];
    self.firstPic = [aDecoder decodeObjectForKey:kAuctionInfoObjFirstPic];
    self.mileageCount = [aDecoder decodeObjectForKey:kAuctionInfoObjMileageCount];
    self.state = [aDecoder decodeObjectForKey:kAuctionInfoObjState];
    self.auctionTitle = [aDecoder decodeObjectForKey:kAuctionInfoObjAuctionTitle];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_vehiclePics forKey:kAuctionInfoObjVehiclePics];
    [aCoder encodeObject:_shelfCode forKey:kAuctionInfoObjShelfCode];
    [aCoder encodeObject:_carColor forKey:kAuctionInfoObjCarColor];
    [aCoder encodeDouble:_internalBaseClassIdentifier forKey:kAuctionInfoObjId];
    [aCoder encodeObject:_startDate forKey:kAuctionInfoObjStartDate];
    [aCoder encodeObject:_startPrice forKey:kAuctionInfoObjStartPrice];
    [aCoder encodeObject:_firstPic forKey:kAuctionInfoObjFirstPic];
    [aCoder encodeObject:_mileageCount forKey:kAuctionInfoObjMileageCount];
    [aCoder encodeObject:_state forKey:kAuctionInfoObjState];
    [aCoder encodeObject:_auctionTitle forKey:kAuctionInfoObjAuctionTitle];
}

- (id)copyWithZone:(NSZone *)zone
{
    AuctionInfoObj *copy = [[AuctionInfoObj alloc] init];
    
    if (copy) {

        copy.vehiclePics = [self.vehiclePics copyWithZone:zone];
        copy.shelfCode = [self.shelfCode copyWithZone:zone];
        copy.carColor = [self.carColor copyWithZone:zone];
        copy.internalBaseClassIdentifier = self.internalBaseClassIdentifier;
        copy.startDate = [self.startDate copyWithZone:zone];
        copy.startPrice = [self.startPrice copyWithZone:zone];
        copy.firstPic = [self.firstPic copyWithZone:zone];
        copy.mileageCount = [self.mileageCount copyWithZone:zone];
        copy.state = [self.state copyWithZone:zone];
        copy.auctionTitle = [self.auctionTitle copyWithZone:zone];
    }
    
    return copy;
}


@end
